<?php
use app\models\Article;
use app\models\ArticleCategory;
use app\models\KeyMap;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $model_list app\models\Article[]
 * @var $pagination yii\data\Pagination
 */

$this->title = '资讯列表';
$this->params['breadcrumbs'][] = '资讯管理';
$this->params['breadcrumbs'][] = $this->title;

app\assets\TableAsset::register($this);
app\assets\AjaxAsset::register($this);
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
    <div class="form-group">
        <label for="search_cid" class="sr-only">Category</label>
        <?php echo Html::dropDownList('search_cid', Yii::$app->request->get('search_cid'), [''=>'搜索分类'] + ArticleCategory::list4Options(), ['id'=>'search_cid', 'class'=>'form-control']);?>
    </div>
    <div class="form-group">
        <label for="search_title" class="sr-only">Title</label>
        <?php echo Html::textInput('search_title', Yii::$app->request->get('search_title'), ['id'=>'search_title', 'class'=>'form-control', 'placeholder'=>'搜索标题']);?>
    </div>
    <div class="form-group">
        <label for="search_status" class="sr-only">Status</label>
        <?php echo Html::dropDownList('search_status', Yii::$app->request->get('search_status'), [''=>'搜索状态',] + KeyMap::getValues('article_status'), ['id'=>'search_status', 'class'=>'form-control']);?>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary btn-sm">搜索</button>
    </div>
    <br />
    <div class="form-group">
        <a href="<?php echo Url::to(['/admin/article/edit']);?>" class="btn btn-success btn-sm">添加</a>
    </div>
<?php echo Html::endForm();?>
<table id="simple-table" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="center">
                <label class="pos-rel">
                    <input type="checkbox" class="ace" />
                    <span class="lbl"></span>
                </label>
            </th>
            <th>分类</th>
            <th>标题</th>
            <th>状态</th>
            <th>添加时间</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($model_list as $model) {?>
            <tr id="data_<?php echo $model->id;?>">
                <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $model->id;?>" /><span class="lbl"><?php echo $model->id;?></span></label></td>
                <td><?php echo empty($model->category) ? $model->cid : $model->category->name;?></td>
                <td><?php echo Html::encode($model->title);?></td>
                <td><?php echo Html::a(KeyMap::getValue('article_status', $model->status), 'javascript:void(0)', ['class'=>[
                        Article::STATUS_DRAFT=>'label label-default',
                        Article::STATUS_WAIT=>'label label-info',
                        Article::STATUS_PUBLISHED=>'label label-success',
                        Article::STATUS_REFUSED=>'label label-warning',
                        Article::STATUS_DELETED=>'label label-error'
                    ][$model->status]]);?>
                    <?php if (!empty($model->remark)) {echo '<br />', Html::encode($model->remark);}?>
                </td>
                <td><?php echo Yii::$app->formatter->asDatetime($model->add_time);?></td>
                <td><?php echo app\widgets\AdminTableOp::widget(['items'=>[
                    ['rbac'=>'article/edit', 'icon' => 'fa fa-pencil', 'btn_class'=>'btn btn-xs btn-success', 'color'=>'green', 'tip'=>'修改', 'href'=>Url::to(['/admin/article/edit', 'id'=>$model->id])],
                    ['rbac'=>'article/edit', 'icon' => 'fa fa-trash', 'btn_class'=>'btn btn-xs btn-danger', 'color'=>'red', 'tip'=>'删除', 'onclick'=>'deleteArticle(' . $model->id . ')'],
                ]]);?></td>
            </tr>
        <?php }?>
    </tbody>
</table>
<?php echo app\widgets\LinkPager::widget(['pagination'=>$pagination]);?>
<script>
/**
 * 删除资讯
 */
function deleteArticle(id) {
    if (!confirm('确定要删除吗？')) {
        return false;
    }
    $.getJSON('<?php echo Url::to(['/admin/article/delete']);?>', {'id':id}, function(json) {
        if (callback(json)) {
            $('#data_' + id).remove();
        }
    });
}
</script>
